home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 501-525 / disk_509 / multi_player / sources / ppdata.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  181 lines

  1. /********************************************************************
  2. *                                                                   *
  3. *  PowerPacker DATA file support function V1.1                      *
  4. *  -------------------------------------------                      *
  5. *                            (Read Packer.doc for more information) *
  6. *                                                                   *
  7. *    error = PP_LoadData (file, col, typeofmem, buffer, length, pw) *
  8. *    with:                                                          *
  9. *       char *file;     filename                                    *
  10. *       UBYTE col;      color (see ppdata.h)                        *
  11. *       ULONG typeofmem type of memory that will be allocated       *
  12. *       UBYTE **buffer  pointer to pointer to buffer                *
  13. *       ULONG *length   pointer to buffer length                    *
  14. *       char *pw;       pointer to password or NULL                 *
  15. *                                                                   *
  16. *  NOTE: - After loading you must free the allocated memory:        *
  17. *          DO NOT FORGET !!!!!                                      *
  18. *             FreeMem (buffer, length);                             *
  19. *        - Errors are defined in ppdata.h                           *
  20. *        - For encrypted data call first with pw = NULL, then       *
  21. *          if error is PP_CRYPTED you know file is crypted.         *
  22. *          Prompt the user for a password and call again with       *
  23. *          pw pointing to this password. If the password is         *
  24. *          incorrect error is PP_PASSERR, otherwise the file will   *
  25. *          be loaded and decrypted.                                 *
  26. *                                                                   *
  27. *    Example:                                                       *
  28. *                                                                   *
  29. *      #include <ppdata.h>                                          *
  30. *      ...                                                          *
  31. *                                                                   *
  32. *      UBYTE *mymem = NULL;                                         *
  33. *      ULONG mylen = 0;                                             *
  34. *                                                                   *
  35. *      err = PP_LoadData ("df0:myfile.pp", DECR_POINTER,            *
  36. *                     MEMF_PUBLIC+MEMF_CHIP, &mymem, &mylen, NULL); *
  37. *      if (err == PP_LOADOK) {                                      *
  38. *         DoSomething (mymem, mylen);                               *
  39. *         FreeMem (mymem, mylen);                                   *
  40. *         }                                                         *
  41. *      else switch (err) {                                          *
  42. *         case PP_CRYPTED:                                          *
  43. *            puts ("File is encrypted !");                          *
  44. *            break;                                                 *
  45. *         case PP_READERR:                                          *
  46. *            puts ("Loading error !!!");                            *
  47. *            break;                                                 *
  48. *         ...                                                       *
  49. *         }                                                         *
  50. *                                                                   *
  51. ********************************************************************/
  52. /********************************************************************
  53. *                                                                   *
  54. *  'PP_LoadData' PowerPacker DATA file support function V1.1        *
  55. *                                                                   *
  56. *  You may use this code for non-commercial purposes provided this  *  
  57. *  copyright notice is left intact !                                *
  58. *                                                                   *
  59. *                          Copyright (c) Aug 1989 by Nico François  *
  60. ********************************************************************/
  61.  
  62. #include <exec/types.h>
  63. #include <exec/io.h>
  64. #include <exec/memory.h>
  65. #include <libraries/dos.h>
  66. /*
  67. #include <functions.h>
  68. */
  69. #include "ppdata.h"
  70.  
  71. #define SAFETY_MARGIN    64L
  72. #define SIZEOF                (ULONG)sizeof
  73. #define myRead(to,len)    if (Read (pp_lock, to, len) != len) {\
  74.                                         pp_FreeStuff(); return (PP_READERR); }
  75. struct FileLock *pp_lock;
  76. struct FileInfoBlock *pp_FileInfoBlock;
  77. UBYTE *pp_filestart;
  78. ULONG pp_bufferlen;
  79. UWORD pp_coladdr[4] = { 0xf180, 0xf182, 0xf1a2, 0xf102 };
  80. UWORD pp_CalcCheckSum();
  81. ULONG pp_CalcPasskey();
  82.  
  83. PP_LoadData (pp_file, color, typeofmem, buffer, length, pw)        /* Version 1.1 */
  84. char *pp_file;
  85. UBYTE color;
  86. ULONG typeofmem;
  87. UBYTE **buffer;
  88. ULONG *length;
  89. char *pw;
  90. {
  91. /*
  92.     ULONG hdr;
  93. */
  94.     char hdr[4];
  95.     ULONG pp_seek;
  96.     UWORD *decrcol, instr, hicol, locol;
  97.     ULONG pp_filelen, pp_crunlen, pp_efficiency;
  98.     UBYTE pp_crunched;
  99.     extern void pp_DecrunchBuffer(), pp_DecrunchColor();
  100.  
  101.     pp_filestart = NULL;
  102.     if (!(pp_FileInfoBlock = (struct FileInfoBlock *)AllocMem
  103.         (SIZEOF(*pp_FileInfoBlock), MEMF_PUBLIC))) return (PP_NOMEMORY);
  104.  
  105.     /* Set decruncher color */
  106.     decrcol = (UWORD *)pp_DecrunchColor;
  107.     if (color != 4) {
  108.         instr = 0x33c9; hicol = 0x00df;
  109.         locol = pp_coladdr[color];                /* = move.w a1,$dff1xx */
  110.         }
  111.     else instr = hicol = locol = 0x4e71;     /* nop */
  112.     *decrcol = instr;
  113.     *(decrcol+1) = hicol; *(decrcol+2) = locol;
  114.  
  115.     if (!(pp_lock = (struct FileLock *)Lock (pp_file, ACCESS_READ))) {
  116.         pp_FreeStuff();
  117.         return (PP_LOCKERR);
  118.         }
  119.     Examine (pp_lock, pp_FileInfoBlock);
  120.     UnLock (pp_lock);
  121.     pp_crunlen = pp_FileInfoBlock->fib_Size;
  122.  
  123.     /* read decrunched length */
  124.     if (!(pp_lock = (struct FileLock *)Open (pp_file, MODE_OLDFILE))) {
  125.         SimpleRequest("Can't Open");
  126.         pp_FreeStuff();
  127.         return (PP_OPENERR);
  128.         }
  129.     myRead (hdr, 4L);
  130.  
  131.     /* check if crunched */
  132. /*
  133.     if ( hdr == 'PP11' || hdr == 'PP20') && (pp_crunlen>16L)) {
  134. */
  135.     if ( 
  136.     (((hdr[0] == 'P') && (hdr[1] == 'P') && (hdr[2] == '2') &&(hdr[3] == '0'))
  137.     || ((hdr[0] == 'P') && (hdr[1] == 'P') && (hdr[2] == '1') &&(hdr[3] == '1')))
  138.      && (pp_crunlen>16L)) {
  139.         pp_seek = 4L;
  140.         Seek (pp_lock, pp_crunlen - 4L, OFFSET_BEGINNING);
  141.         myRead (&pp_filelen, 4L);
  142.         pp_filelen >>= 8L;
  143.         pp_crunlen -= 4L + pp_seek;
  144.         Seek (pp_lock, pp_seek, OFFSET_BEGINNING);
  145.         myRead (&pp_efficiency, 4L);
  146.         pp_bufferlen = pp_filelen + SAFETY_MARGIN;
  147.         pp_crunched = TRUE;
  148.         }
  149.     else {
  150.         Seek (pp_lock, 0L, OFFSET_BEGINNING);
  151.         pp_bufferlen = pp_filelen = pp_crunlen;
  152.         pp_crunched = FALSE;
  153.         }
  154.     if (!(pp_filestart=(UBYTE *)AllocMem (pp_bufferlen, typeofmem))) {
  155.         SimpleRequest("Sorry,not enough mem...");
  156.         pp_FreeStuff();
  157.         return (PP_NOMEMORY);
  158.         }
  159.     /* load file */
  160.     myRead (pp_filestart, pp_crunlen);
  161.  
  162.     Close (pp_lock);
  163.     FreeMem (pp_FileInfoBlock, SIZEOF(*pp_FileInfoBlock));
  164.     if (pp_crunched) {
  165.         pp_DecrunchBuffer (pp_filestart + pp_crunlen,
  166.                                             pp_filestart + SAFETY_MARGIN, pp_efficiency);
  167.         FreeMem (pp_filestart, SAFETY_MARGIN);
  168.         pp_filestart += SAFETY_MARGIN;
  169.         }
  170.     *buffer = pp_filestart;
  171.     *length = pp_filelen;
  172.     return (PP_LOADOK);
  173. }
  174.  
  175. pp_FreeStuff()
  176. {
  177.     if (pp_lock) Close (pp_lock);
  178.     if (pp_filestart) FreeMem (pp_filestart, pp_bufferlen);
  179.     if (pp_FileInfoBlock) FreeMem (pp_FileInfoBlock, SIZEOF(*pp_FileInfoBlock));
  180. }
  181.